const {
  reply
} = require('../../wechat/reply')
const api = require('../api/index')
const config = require('../../config/config')
const wechatMiddle = require('../../wechat-lib/middleware')
// 介入微信消息中间件
exports.hear = async (ctx, next) => {
  const middle = wechatMiddle(config.wx, reply)
  await middle(ctx, next);
}
// 网页授权 跳转code
exports.oauth = async ctx => {
  const target = config.baseURL + '/userinfo'
  const scope = 'snsapi_userinfo'
  const state = ctx.query.id;
  const url = api.wechat.getAuthorizeURL(target, state, scope);
  ctx.redirect(url);
}
// 网页授权 用户信息
exports.userinfo = async (ctx, next) => {
  const userData = await api.wechat.getUserinfoByCode(ctx.query.code)
  ctx.body = userData
}

// 
exports.sdk = async (ctx, next) => {
  const url = ctx.href;
  const params = await api.wechat.getSignature(url)
  await ctx.render('index', params)
}

exports.goods = async (ctx, next) => {
  await ctx.render('goods')
}
